﻿app.factory('PizzaService', ['$http', '$q', function ($http, $q) {
    //CRUD
    var createPizza = function (name, size, toppings, crust) {
        var newPizza = {
            name: name,
            size: size,
            toppings: toppings,
            crust: crust
        };

        var deferred = $q.defer();

        $http({
            url: 'https://nickspizzaapp.firebaseio.com/.json',
            method: 'POST',
            data: newPizza
        }).success(function (data) {
            deferred.resolve(data);
        }).error(function () {
            deferred.reject();
        });

        return deferred.promise;
    };

    var getPizzas = function () {
        var deferred = $q.defer();

        $http({
            url: 'https://nickspizzaapp.firebaseio.com/.json',
            method: 'GET'
        }).success(function (data) {
            deferred.resolve(data);
        }).error(function () {
            deferred.reject();
        });

        return deferred.promise;
    };

    return {
        createPizza: createPizza,
        getPizzas: getPizzas
    };
}]);